<?php
/*
	我的下线
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
set_html('title','我的团');
if($_RQ['op']=='main'){
	if(!$this->setting['isopen']){
		exi('未开启分销！','error');
	}
	$return['setting']=$this->plugin_setting('page');
	set_html('tabs',$return['setting']['agent_tagent']?:'我的团');
	$setting=$this->plugin_setting('setting');
	$levelnum=3;
	$agent_ids=$_SESSION['uid'];
	$agent_num=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$_SESSION['uid']),array('mid'),'mid');
	$levels=array();
	for($i=0;$i<$levelnum;$i++){
		$level=$i+1;
		if($i==0){
			$levels[$level]['title']='直推用户';
			$levels[$level]['num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$_SESSION['uid']),array('mid'),'mid');
		}elseif($i==1){
			$levels[$level]['title']='间推用户';
			$mids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$_SESSION['uid']),array('mid'),'mid');
			if($mids){
				$mids=array_keys($mids);
			}else{
				$mids=-1;
			}
			$levels[$level]['num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mids),array('mid'),'mid');
		}elseif($i==2){
			$levels[$level]['title']='贡献用户';
			$levels[$level]['num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'gei_mid'=>$_SESSION['uid']),array('mid'),'mid');
		}
	}
	$return['levels']=$levels;
	$return['agent_num']=$agent_num;
	$return['member']=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	$return['params']=array('page'=>1,'level'=>1);
	$return['psize']=20;
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	$level=intval($_RQ['level']);
	if(!$level){
		$level=1;
	}
	if($level==1){
		$params['fromuser']=$_SESSION['uid'];
	}elseif($level==2){
		$mids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$_SESSION['uid']),array('mid'),'mid');
		if($mids){
				$mids=array_keys($mids);
		}else{
				$mids=-1;
		}
		$params['fromuser']=$mids;
	}else{
		$params['gei_mid']=$_SESSION['uid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
	);
	$list=$this->get_list('member',$parameter);
	if(!$list){
		exi('none');
	}
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['member_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
		$item['check_time']=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'check_time');
		$item['ordercount']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
		$item['moneycount']=$this->getsum('order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'order_price');
	}
	exi($list);
}
?>